#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
A module to calculate scatering angle. 
Now this module works only when passed not plains but normals.
'''


from plane import Plane
def calculate_scateringangle(aplane, adir, bplane, bdir):
    # pplane is a plane in which lies m and d
    pplane = aplane.normal().get_perpendicular_between(bplane.normal()).normal()
    # chek if planes is directed appart each other
    ppA = max((pplane.dir)%360, (pplane.dir+180)%360)
    ppB = ppA - 180
    t = [(adir < ppA and adir > ppB), (bdir < ppA and bdir > ppB)]
    isDirAppart = True
    if t == [True, True] or t==[False, False]: isDirAppart = False
    # chek if planes is directed in one half or different
    ppC = max((pplane.dir+90)%360, (pplane.dir-90)%360)
    ppD = ppC - 180
    t = [(adir < ppC and adir > ppD), (bdir < ppC and bdir > ppD)]
    isOneHalf = False
    if t == [True, True] or t == [False, False]: isOneHalf = True
    # find angle between d-scatdir , m-scatdir and pplane
    mscatplane = aplane.normal().get_perpendicular_between(Plane(adir, .001)).normal()
    dscatplane = bplane.normal().get_perpendicular_between(Plane(bdir, .001)).normal()
    mscatangle = mscatplane.normal().return_angle_between(pplane.normal())
    dscatangle = dscatplane.normal().return_angle_between(pplane.normal())
    if mscatangle > 90: mscatangle = 180 - mscatangle
    if dscatangle > 90: dscatangle = 180 - dscatangle
    return isDirAppart, isOneHalf, mscatangle, dscatangle
